package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.enmu.DeletedEnum;
import com.product.enmu.OperatorType;
import com.product.entity.AssemblyLine;
import com.product.mapper.AssemblyLineMapper;
import com.product.service.IAssemblyLineService;
import com.product.service.IOperationLogService;
import com.product.to.AddAssemblyLineTo;
import com.product.to.UpdateAssemblyLineTo;
import com.product.utils.PageUtils;
import com.product.vo.AssemblyLineVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AssemblyLineServiceImpl extends ServiceImpl<AssemblyLineMapper, AssemblyLine> implements IAssemblyLineService {
    @Autowired
    private IOperationLogService operatorService;

    @Override
    public void deleteAssemblyLine(Integer assemblyLineId) {
        LambdaUpdateWrapper<AssemblyLine> updateWrapper = new LambdaUpdateWrapper<AssemblyLine>()
                .eq(AssemblyLine::getLineId, assemblyLineId)
                .set(AssemblyLine::getDeleted, DeletedEnum.DELETE.getValue());
        this.baseMapper.update(updateWrapper);

        operatorService.addOperation(OperatorType.Delete.getCode(), "删除AssemblyLine表");

    }

    @Override
    public void deleteAssemblyLines(List<Integer> assemblyLineIds) {
        for (Integer assemblyLineId : assemblyLineIds) {
            deleteAssemblyLine(assemblyLineId);
        }
    }

    @Override
    public void updateAssemblyLine(UpdateAssemblyLineTo assemblyLine) {
        AssemblyLine newAssemblyLine = new AssemblyLine();
        BeanUtils.copyProperties(assemblyLine, newAssemblyLine);
        this.baseMapper.updateById(newAssemblyLine);

        operatorService.addOperation(OperatorType.Update.getCode(), "修改AssemblyLine表");
    }

    @Override
    public void addAssemblyLine(AddAssemblyLineTo assemblyLine) {
        AssemblyLine newAssemblyLine = new AssemblyLine();
        BeanUtils.copyProperties(assemblyLine, newAssemblyLine);
        newAssemblyLine.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(newAssemblyLine);

        operatorService.addOperation(OperatorType.Insert.getCode(), "添加AssemblyLine表");
    }

    @Override
    public PageUtils getAllAssemblyLine(Integer size, Integer current, String lineName, Integer orderItemId, Integer timeOrderBy) {
        Page<AssemblyLine> assemblyLinePage = new Page<>(current, size);
        LambdaQueryWrapper<AssemblyLine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(lineName != null, AssemblyLine::getName, lineName);
        queryWrapper.eq(orderItemId != null, AssemblyLine::getOrderItemId, orderItemId);
        queryWrapper.orderBy(timeOrderBy != null, timeOrderBy != null && timeOrderBy == 0, AssemblyLine::getUpdateTime);

        Page<AssemblyLine> page = this.baseMapper.selectPage(assemblyLinePage, queryWrapper);

        List<AssemblyLineVo> assemblyLineVoList = page.getRecords().stream().map(item -> {
            AssemblyLineVo assemblyLineVo = new AssemblyLineVo();
            BeanUtils.copyProperties(item, assemblyLineVo);
            return assemblyLineVo;
        }).toList();
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(assemblyLineVoList);
        return pageUtils;
    }
}
